-- Basic application bootstraper
local name, path_info = cgilua.splitonfirst(cgilua.script_vpath)

CGILUA_APPS = KEPLER_APPS.."/cgilua"

if name == nil or name == "" then
    if io.open("index.lua") then
        cgilua.handle("index.lua")
    elseif io.open("index.lp") then
        cgilua.handle("index.lp")
    else
        error("No script defined")
    end
else
	local appdir = CGILUA_APPS.."/"..name
	local appinit = "init.lua"
	lfs.chdir(appdir)

	if io.open(appinit) then
        -- prepares the application environment
	    package.path = CGILUA_APPS.."/"..name.."/lua/?.lua;"..package.path
	    cgilua.script_pdir = CGILUA_APPS.."/"..name
	    cgilua.script_file = nil
	    cgilua.script_vdir = "/"
	    cgilua.urlpath = cgilua.urlpath.."/"..name
	    cgilua.script_vpath = path_info
	    cgilua.app_name = name
		cgilua.doif(appinit)
	else
		cgilua.handle(cgilua.script_pdir.."/"..name..path_info)
	end
end